// 共享类型定义
export enum TaskStatus {
    PENDING = 'pending',
    RUNNING = 'running',
    COMPLETED = 'completed',
    FAILED = 'failed',
    CANCELLED = 'cancelled',
}

export enum TaskPriority {
    LOW = 0,
    NORMAL = 5,
    HIGH = 10,
    CRITICAL = 15,
}

export interface TaskOptions {
    priority?: TaskPriority;
    timeout?: number;
    retryCount?: number;
    retryDelay?: number;
}

export type TaskFunction<T = any> = () => Promise<T>;

export interface Task<T = any> {
    readonly id: string;
    readonly name: string;
    readonly taskFunction: TaskFunction<T>;
    readonly priority: TaskPriority;
    readonly createdAt: number;
    readonly options: Required<TaskOptions>;
    status: TaskStatus;
    startedAt?: number;
    completedAt?: number;
    error?: Error;
    retryAttempts: number;
}

export interface TaskResult<T = any> {
    /** 任务id */
    readonly id: string;
    /** 任务名称 */
    readonly name: string;
    /** 任务结果 */
    readonly data: T;
    /** 执行时间（毫秒） */
    readonly executionTime: number;
    readonly timestamp: number;
}

export interface QueueConfig {
    concurrency: number;
    defaultTimeout: number;
    maxRetries: number;
    retryDelay: number;
    cacheMaxAge: number;
    enableIdleCallback: boolean;
    enableCache: boolean;
}

export interface QueueStats {
    pending: number;
    running: number;
    completed: number;
    failed: number;
    cancelled: number;
    totalProcessed: number;
    averageExecutionTime: number;
    cacheHitRate: number;
}

export type TaskEventType =
    | 'taskStarted'
    | 'taskCompleted'
    | 'taskFailed'
    | 'taskCancelled'
    | 'queueStarted'
    | 'queueEmpty';

export interface TaskEvent<T = any> {
    type: TaskEventType;
    /** task事件存在 */
    task: Task<T>;
    result?: TaskResult<T>;
    timestamp: number;
}

export type TaskEventListener<T = any> = (event: TaskEvent<T>) => void;

// 查询相关接口
export interface TaskQuery {
    id?: string;
    name?: string;
    status?: TaskStatus;
    priority?: TaskPriority;
    createdAfter?: number;
    createdBefore?: number;
}

export interface ResultQuery {
    taskId?: string;
    taskName?: string;
    executionTimeMin?: number;
    executionTimeMax?: number;
    timestampAfter?: number;
    timestampBefore?: number;
}

export type TaskChangeListener<T = any> = (task: Task<T>) => void;

export interface TaskExecuteOption {
    /**
     * 是否立即执行
     */
    immediate: boolean;
}
